<?php
class Data_Model_DiemThiMapper extends Data_Model_DataMapperAbstract
{
	protected static $_instance = null;
	// dung de phan trang
	private $_paginator = null;
	
	
	public function searchByMahocvien($id)
	{
		$db = $this->getDb();
		$selectDiemthi = $db->select()
							->from('tbl_diemthi')
							->where('MaHocVien = ?',$id);
		$diemthis = $db->fetchAll($selectDiemthi);
		
		$diemthiArray = array();
		foreach($diemthis as $diemthi)
		{
		$hocvienMapper = Data_Model_HocVienMapper::getInstance();
		$hocvien = $hocvienMapper->searchById($id);
		$diemthi['tbl_hocvien'] = $hocvien;
		
		$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
		$khoahoc = $khoahocMapper->searchByMakhoahoc($diemthi['MaKhoaHoc']);
		$diemthi['tbl_khoahoc'] = $khoahoc;
		
		$diemthiArray[] = $this->_populate($diemthi);
		}
		return $diemthiArray;
	}
	public function checkByMahv($makh,$id)
	{
		$db = $this->getDb();
		$selectDiemthi = $db->select()
							->from('tbl_diemthi')
							->where('MaKhoaHoc = ?',$makh)
							->where('MaHocVien = ?',$id);
		$diemthi = $db->fetchRow($selectDiemthi);
		return $diemthi;
	}	
	public function searchByMakhoahoc($id)
	{
		$db = $this->getDb();
		$selectDiemthi = $db->select()
							->from('tbl_diemthi')
							->where('MaKhoaHoc = ?',$id);
		$diemthis = $db->fetchAll($selectDiemthi);
		
		$diemthiArray = array();
		foreach($diemthis as $diemthi)
		{
			
		$hocvienMapper = Data_Model_HocVienMapper::getInstance();
		$hocvien = $hocvienMapper->searchById($diemthi['MaHocVien']);
		$diemthi['tbl_hocvien'] = $hocvien;
		
		$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
		$khoahoc = $khoahocMapper->searchById($id);
		$diemthi['tbl_khoahoc'] = $khoahoc;

		$diemthiArray[] = $this->_populate($diemthi);
		}
		return $diemthiArray;	
	}
	// hien thi
	public function _populate($data)
	{
		$obj = new Data_Model_DiemThi();
		$obj->setId($data['MaBangDiem']);
		$obj->setMakhoahoc($data['MaKhoaHoc']);
		$obj->setMahocvien($data['MaHocVien']);
		$obj->setDiem($data['Diem']);
		$obj->setKetqua($data['KetQua']);
		$obj->setHocvien($data['tbl_hocvien']);
		$obj->setKhoahoc($data['tbl_khoahoc']);
		
		return $obj;
	}
	protected function _insert(Data_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		$data = array(
				'MaKhoaHoc' => $obj->getMakhoahoc(),
				'MaHocVien' => $obj->getMahocvien(),
				'Diem' => $obj->getDiem(),
				'KetQua' => $obj->getKetqua()
				);
		$db->insert('tbl_diemthi',$data);
	}
	protected function _update(Data_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data = array(
				'MaKhoaHoc' => $obj->getMakhoahoc(),
				'MaHocVien' => $obj->getMahocvien(),
				'Diem' => $obj->getDiem(),
				'KetQua' => $obj->getKetqua()
		);
		$where = $db->quoteInto('MaBangDiem = ?', $obj->getId());
		$db->update('tbl_diemthi',$data,$where);
	}
	public function updateBd($id,$makhoahoc,$mahocvien,$diem,$ketqua)
	{
		$db = $this->getDb();
		$data = array(
				'MaKhoaHoc' => $makhoahoc,
				'MaHocVien' => $mahocvien,
				'Diem' => $diem,
				'KetQua' => $ketqua
		);
		$where = $db->quoteInto('MaBangDiem = ?', $id);
		$db->update('tbl_diemthi',$data,$where);
	}
	public function deleteByMahocvien($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaHocVien = ?',$id);
		$db->delete('tbl_diemthi',$where);
	}
	
	// Goi chinh no
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();				
		}
		return self::$_instance;
	}
}